package net.sureon.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.UserSession;
import net.sureon.common.SureonConstants;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class Logout implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        UserSession sess = (UserSession) request.getSession().getAttribute(
            UserSession.HTTP_SESSION_KEY);
        if (sess != null) {
            sess.reset();
        }
        request.getSession().removeAttribute(UserSession.HTTP_SESSION_KEY);
        request.getSession().removeAttribute(SureonConstants.SESSION_ATTR_COMPAYCONFIG);
        AppCookie cookie = new AppCookie(request.getCookies());
        cookie.resetToken(response);
        ModelAndView view = new ModelAndView("logout");
        view.addObject("indexPageUrl", request.getContextPath() + "/index.do");
        return view;
    }
}
